<?php
namespace App\Http\Controllers;
/**
 * 
 * 用户账户控制器
 *
 *
 */
use App\Models\Account;
use App\Models\AccountTakeApply;
use App\Services\UserAccountService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class UserAccountController extends BaseController {

    /**
     * 提现申请
     * @param $user_id
     * @param UserAccountService $accountService
     * @return mixed
     * @throws \Exception
     */
    function postTakeCashApply($user_id,UserAccountService $accountService){

        if(!$user_id){
            return $this->args_invalid();
        }

        //获取提现配置信息
        $take_apply_min_money = DB::table('config')->where('code','take_apply_min_money')->value('value');//最低提现金额 配置
        $take_apply_fee = DB::table('config')->where('code','take_apply_fee')->value('value');//手续费 配置

        $input = Input::all();

        $rules = array(
            'amount'  => 'required', //提现金额
            'bank_name'  => 'required',  //银行名称
            'account'  => 'required',  //账户
            'owner_name'  => 'required',  //户名
            'branch'  => 'required',  //开户行
        );


        $validation = Validator::make($input, $rules);

        if ($validation->fails()){
            return $this->failure($validation->messages());
        }

        //参数
        $amount = Input::get('amount');
        $bank_name= Input::get('bank_name');
        $account= Input::get('account');
        $owner_name = Input::get('owner_name');
        $branch = Input::get('branch');

        if($amount < $take_apply_min_money){
            return $this->failure('最低提现金额不可小于'.$take_apply_min_money);
        }

        // 验证账户信息
        $user_account = Account::where('account_id',$user_id)
            ->first();

        if(!$user_account){
            return $this->failure('账户不存在');
        }
        if($user_account->money < $amount){
            return $this->failure('余额不足！');
        }

        DB::beginTransaction();

        try {
            //申请信息入库
            $apply = new AccountTakeApply();
            $apply->account_id = $user_id;
            $apply->take_type =1;
            $apply->amount =$amount;
            $apply->takecash_fee = $take_apply_fee*$amount/100;
            $apply->create_at = gmtime();
            $apply->update_at = gmtime();
            $apply->app_status = 0;
            $apply->app_result = '';
            $apply->bank_name = $bank_name;
            $apply->account = $account;
            $apply->owner_name = $owner_name;
            $apply->branch = $branch;
            $apply->save();

            //冻结资金入库
            $accountService->frozenMoney($user_id,$amount,'提现申请',false);

            DB::commit();

        } catch(\Exception $e){
            DB::rollback();
            throw $e;
        }

        return $this->success();

    }

    /**
     * 提现处理
     * @param $user_id
     * @param $apply_id
     * @param UserAccountService $accountService
     * @return mixed
     * @throws \Exception
     */
    function postTakeCashApproval($user_id,$apply_id,UserAccountService $accountService){

        if(!$user_id){
            return $this->args_invalid();
        }

        //参数
        $app_status = Input::get('app_status'); //1-同意 2-拒绝
        $app_result = Input::get('app_result'); //操作描述

        if(!$app_status){
            return $this->args_invalid();
        }

        // 获取用户账号信息
        $user_account = Account::where('account_id',$user_id)
            ->first();
        if(!$user_account){
            return $this->failure('账户不存在');
        }
        //获取用户提现申请数据
        $apply =AccountTakeApply::find($apply_id);
        if(!$apply){
            return $this->failure('提现申请数据不存在');
        }

        if($app_status == 1){

            DB::beginTransaction();

            try {
                //同意申请 修改申请状态

                $apply->update_at =gmtime();
                $apply->app_status = 1;
                $apply->app_result = $app_result;
                $apply->save();

                //同意申请 扣除提现金额
                $accountService->AgreeTakeApply($user_account->account_id,$apply->amount,'提现审核通过',false);
                //平台收取手续费
                if($apply->takecash_fee>0){

                    $admin_account = Account::where('account_id',1)->first();
                    if($admin_account){

                        $accountService->incomeMoney($admin_account->account_id,$apply->takecash_fee,'提现申请ID:'.$apply->id,$apply->id,'takeapply','+');
                    }

                }


                DB::commit();

            } catch(\Exception $e){
                DB::rollback();
                throw $e;
            }

        } else {
            //不同意申请
            DB::beginTransaction();

            try {
                //不同意申请 修改申请状态

                $apply->update_at = gmtime();
                $apply->app_status = -1;
                $apply->app_result = $app_result;
                $apply->save();

                //冻结资金退回用户余额
                $accountService->unfrozen($user_account->account_id,$apply->amount,'提现审核拒绝',false);

                DB::commit();

            } catch(\Exception $e){
                DB::rollback();
                throw $e;
            }

        }

        return $this->success();

    }
    
}
